package com.cn.intelligence.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbMUserMapper;
import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbMUser;
import com.cn.intelligence.pojo.TbMUserExample;
import com.cn.intelligence.service.SytemUserService;
import com.cn.intelligence.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class SytemUserServiceImpl implements SytemUserService {

	@Autowired
	public TbMUserMapper tbMUserMapper;

	/**
	 * 获取系统用户
	 */
	@Override
	public EUDataGridResult getSystemUserList(Integer page, Integer rows) {
		TbMUserExample tbMUserExample = new TbMUserExample();
		// 分页查询
		PageHelper.startPage(page, rows);
		List<TbMUser> tbMuserList = tbMUserMapper.selectByExample(tbMUserExample);
		// 获取总数
		PageInfo<TbMUser> pageInfo = new PageInfo<TbMUser>(tbMuserList);
		// 实现返回值设定
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setRows(tbMuserList);
		euDataGridResult.setTotal(pageInfo.getTotal());
		return euDataGridResult;

	}

	/**
	 * 添加修改用户信息
	 */
	@Override
	public TaotaoResult insertTbMuser(TbMUser tbMuser) {
		TbMUserExample tbMUserExample = new TbMUserExample();
		TbMUserExample.Criteria criteria = tbMUserExample.createCriteria();
		int value = 0;
		if(tbMuser.getId() == null || "".equals(tbMuser.getId())){
			long ids = IDUtils.genItemId();
			tbMuser.setId(ids);
			tbMuser.setCreated(new Date());
			tbMuser.setUpdated(new Date());
			value = tbMUserMapper.insert(tbMuser);
		}else{
			criteria.andIdEqualTo(tbMuser.getId());
			TbMUser exittbMuser = tbMUserMapper.selectByPrimaryKey(tbMuser.getId());	
			if(exittbMuser != null){
				tbMuser.setUpdated(new Date());
				value = tbMUserMapper.updateByExampleSelective(tbMuser, tbMUserExample);	
			}else{
				return TaotaoResult.fail();
			}
		}
		if(value == 0){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	
	}

	@Override
	public TaotaoResult deleteUser(Long[] ids) {
		TbMUserExample tbMUserExample = new TbMUserExample();
		TbMUserExample.Criteria criteria = tbMUserExample.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		int value = tbMUserMapper.deleteByExample(tbMUserExample);	
		if(value == ids.length){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	}

	@Override
	public EUDataGridResult getSystemUser(String username, int page, int rows) {
		TbMUserExample tbMUserExample = new TbMUserExample();
		TbMUserExample.Criteria criteria = tbMUserExample.createCriteria();
		criteria.andUsernameLike(username);
		// 分页查询
		PageHelper.startPage(page, rows);
		List<TbMUser> tbMuserList = tbMUserMapper.selectByExample(tbMUserExample);
		// 获取总数
		PageInfo<TbMUser> pageInfo = new PageInfo<TbMUser>(tbMuserList);
		// 实现返回值设定
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setRows(tbMuserList);
		euDataGridResult.setTotal(pageInfo.getTotal());
		return euDataGridResult;

	}

}
